home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / ANSIshellƒ / os_mac_command.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-18  |  3.0 KB  |  136 lines  |  [TEXT/R*ch]

  1. /* os_mac_command.c */
  2. /* 12Jul94 e */
  3.  
  4. #include "os_mac.h"
  5. #include "os_mac_eventchk.h"
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #if defined(THINK_C)
  9. #include <console.h>
  10. #elif defined(__MWERKS__)
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <console.h>
  14. #endif
  15.  
  16. /* "stolen" from Symantec... */
  17.  
  18. /*
  19.  *  parse - divide command line into "words"
  20.  *
  21.  *  Words are delimited by one or more spaces.  Any characters within
  22.  *  matching single (') or double (") quotes are taken literally.  Any
  23.  *  character preceded by a backslash (\) is taken literally.
  24.  *
  25.  */
  26.  
  27. static int
  28. parse(unsigned char *s, unsigned char *t, unsigned char *argv[], int max_nargs)
  29. {
  30.     int c, quote = 0, argc = 0;
  31.         
  32.     while( (c = *s++) != 0 ) {
  33.         if (c <= ' ') // was ==
  34.             continue;
  35.         if (argc < max_nargs)
  36.             argv[argc++] = t;
  37.         do {
  38.             if (c == '\\' && *s)
  39.                 c = *s++;
  40.             else if (c == '"' || c == '\'') {
  41.                 if (!quote) {
  42.                     quote = c;
  43.                     continue;
  44.                 }
  45.                 if (c == quote) {
  46.                     quote = 0;
  47.                     continue;
  48.                 }
  49.             }
  50.             *t++ = c;
  51.         } while (*s && ((c = *s++) > ' ' || quote)); // > was !=
  52.         *t++ = 0;
  53.     }
  54.     return(argc);
  55. }
  56.  
  57. #if defined(THINK_C)
  58. WindowPeek console;
  59. extern WindowPeek cflush(FILE *fp);
  60. #endif
  61.  
  62. #if 0
  63.     SysEnvRec sysenv;
  64.     KeyMap kMap;
  65.     long len;
  66.     short err, i;
  67.     short refNum;
  68.  
  69.     /* make sure System 6 or later is running */
  70.     if ( GetTrapAddress(_SysEnvirons) == GetTrapAddress(_Unimplemented) )
  71.         die("\ppfshell requires at least System 6");
  72.     /* make sure it's a M68020+ and has an FPU */
  73.     SysEnvirons( curSysEnvVers, &sysenv );
  74.     if ( (sysenv.processor < env68020) || (!sysenv.hasFPU) )
  75.         die("\ppfshell requires at least a M68020 and FPU");
  76.     /* added this line for Soren/Quadra...   10Feb92  e  */
  77.     SetApplLimit((void *)(((long )&sysenv)-32768L-2000L));
  78.     /* */
  79. #endif
  80.  
  81. #define NARGS 255
  82.  
  83. int
  84. ecommand(unsigned char ***av)
  85. {
  86.     short i, argc;
  87.     unsigned char buf[4096];
  88.     unsigned char *argbuf;
  89.     unsigned char *argv[NARGS+1];
  90.  
  91. #if defined(THINK_C)
  92.     console = cflush(stdout);
  93. #elif defined(__MWERKS__)
  94.     fputs("? ", stdout);
  95.     fflush(stdout);
  96. #else
  97. "unknown compiler"
  98. #endif
  99.     // done in uio.c now: for (i=0; i<20; i++) os_event_check(); /* let any open apple events in */
  100. #if defined(THINK_C)
  101.     fputs("? ", stdout);
  102.     cflush(stdout);
  103.     while ( ( i = os_console_read( console, buf, 4095 ) ) == 0 )
  104.       os_event_check();
  105. #elif defined(__MWERKS__)
  106.     fflush(stdout);
  107.     while ( ( i = ReadCharsFromConsole( (char *)buf, 4095 ) ) == 0 )
  108.       os_event_check();
  109. #else
  110. "unknown compiler"
  111. #endif
  112.     buf[i] = 0;
  113.     /* 18Dec94 e
  114.     for ( argc = 0; argc < i; argc++ )
  115.       if ( buf[argc] < ' ' ) buf[argc] = ' ';
  116.     sprintf(buf, "%s", argbuf);
  117.     */
  118.     argbuf = (unsigned char *)NewPtr(i+1);
  119.     if(argbuf)
  120.     { argc = parse(buf, argbuf, argv, NARGS);
  121.       *av = (unsigned char **)NewPtrClear(4+(argc<<2));
  122.       if (*av)
  123.       { errno = ((argc >= NARGS) ? -108 : 0); memcpy( *av, argv, argc<<2 ); }
  124.       else
  125.         errno =  -108;
  126.     }
  127.     else
  128.     { argc = 0;
  129.       *av = 0;
  130.       errno = -108;
  131.     }
  132.     return(argc);
  133. }
  134.  
  135. /* end */
  136.